package databases

import (
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"user/model"
)

var DB *gorm.DB

func init() {
	var err error
	DB, err = gorm.Open(mysql.Open("root:@tcp(127.0.0.1:3306)/wsyx?charset=utf8mb4&parseTime=True&loc=Local"), &gorm.Config{})
	if err != nil {
		panic("failed to connect database")
	}
	createTable()
}

func createTable() {
	if !DB.Migrator().HasTable(&model.User{}) {
		err := DB.Migrator().CreateTable(&model.User{})
		if err != nil {
			panic("failed to connect database")
		}
	}
	if !DB.Migrator().HasTable(&model.UserStarGood{}) {
		err := DB.Migrator().CreateTable(&model.UserStarGood{})
		if err != nil {
			panic("failed to connect database")
		}
	}

}
